<?php defined( '_JEXEC' ) or die( 'Restricted access' ); ?><?php defined( '_SEXEC' ) or die( 'Restricted access' ); ?>
<?php
	if(!isset($_SESSION['username']) && !isset($_SESSION['usertype']))
		header("location: index.php");
?>
<?php	

if(isset($submit))
{
	switch(trim($submit))
	{
		case "Save":
				b($destination,"Enter Destination Name");
				if(count($arr) == 0) {
					$sql="insert into destinations (destination,destination_type,status) values(\"$destination\",\"$destination_type\",\"$status\")";
					mysql_query($sql);
					$msg = "Destination added succesful";
					$destination=$destination_type=$status="";
				}
				break;
		case "Update":
				if($step == 2) {
					b($destination,"Enter Destination Name");
					if(count($arr) == 0) {
						$sql="update destinations set destination=\"$destination\", destination_type=\"$destination_type\", status=\"$status\" where id = '$id'";
						mysql_query($sql);
						$msg = "Destination updated succesful";
						$destination=$destination_type=$status="";
					} else {
				  		$btval="     Update     ";
				 	}
				} 
				else if(isset($eid)) {
					$sql="select * from destinations where id = '$eid'";
					$result = mysql_query($sql);
					$row = mysql_fetch_array($result);
					$id = $row['id'];
					$destination = $row['destination'];
					$destination_type = $row['destination_type'];
					$status = $row['status'];
					$step=2;
					$btval="     Update     ";
				}
				break;
		case "Delete":
				$sql="delete from destinations where id = '$eid'";
				mysql_query($sql);
				$msg = "Destination delete succesful";
				break;
	}
}

if(!isset($btval))
	$btval="     Save     ";
	


?>

<div id="title">Destination Management</div>

<form name="destination-form" method="post" action="">
<table border="0" cellspacing="3" cellpadding="3" align="center">
	<tr>
		<td colspan=2>
			<?php
				se();
			?>
		</td>
	</tr>
	<tr>
		<td align=right>Destination Name:</td>
		<td><input name="destination" type="text" value="<?php echo $destination?>"/></td>
	</tr>
	<tr>
		<td align=right>Destination Type:</td>
		<td>
                    <select name="destination_type" style="width:144px;">
			<?php
				sopt($destination_type_arr, $destination_type)
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td align=right>Status:</td>
		<td>
			<?php
				if($status == "")
					$status = 1;
				foreach($status_arr as $k => $v) {
					$st = ($status == $k) ? 'checked' : '';
					echo "&nbsp;&nbsp;<input type='radio' name='status' value='$k' $st> ".$v;
				}
			
			?>
		</td>
	</tr>
	<tr>
		<td colspan="2" align="center">
			<input type=hidden name=id value="<?php echo $id?>" />
			<input type=hidden name=step value="<?php echo $step?>" />
			<input type="submit" name="submit" value="<?php echo $btval?>" class='button'/>
		</td>
	</tr>	
</table>
</form>

<?php
echo "<hr />";
$table = "destinations";
$cols = "*";
$orderby = ($orderby == '') ? "destination" : $orderby;
$sort = ($sort == '') ? "asc" : $sort;
$pagelength = 50;
$reparr = array();
foreach($destination_type_arr as $k => $v){
	$reparr['destination_type'][$k] = $v;	
}
foreach($status_arr as $k => $v){
	$reparr['status'][$k] = $v;	
}
gettable("$table",$cols,"$where","$orderby","$sort",$pg,"<a href='?eid=\$id&submit=Update' style='text-decoration:none'>Update</a>&nbsp;|&nbsp;<a href=\"?eid=\$id&submit=Delete\" onclick=\"javascript:return confirm('Are you sure you want to Delete?')\" style='text-decoration:none'>Delete</a>",Array("id"),$reparr);
?>